package com.exmaple.loadBalance.impl;

import com.exmaple.dto.RpcPing;
import com.exmaple.dto.RpcRequest;
import com.exmaple.loadBalance.RpcLoadBalance;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class RpcLoadBalanceRandomStrategy implements RpcLoadBalance {

    private Random random = new Random();

    @Override
    public RpcPing route(Map<String, RpcPing> pingMap, RpcRequest rpcRequest) {
        if (!pingMap.isEmpty()) {
            List<String> keys = new ArrayList<>(pingMap.keySet());
            String key = keys.get(random.nextInt(keys.size()));
            return pingMap.get(key);
        }
        return null;
    }
}
